:- dynamic get_newbase/3, get_newbase/4, get_newbasename/3.
:- export get_newbase/3, get_newbase/4, get_newbasename/3.



%%%%%% CONVERT TO NEW BASE FACT %%%%%%%%%%%%%%%%%%%%%%%%%%% 

%% get_newbase(p(a,b,c,d,e,f),[[1,5,6],[2,4],[3]],NewBase),
%% NewBase = p_6_newbase_156243(a,e,f,b,d,c)
get_newbase(OriginalBase, IndexArgNumbers, NewBase) :-
	OriginalBase =.. [BaseName|Args],
	functor(OriginalBase,_,Arity),
	(
	\+'_$_$_mtindex'(Functor,Arity,NewBaseName,IndexArgOrderSets,IndexRuleHeads) ->
		get_newbasename(OriginalHead,IndexArgNumbers,NewBaseName)
	),
	get_newbase(NewBaseName, Args, IndexArgNumbers, NewBase).
	
get_newbase(NewBaseName, Args, IndexArgNumbers, NewBase) :- 
	rearrange_args_list(IndexArgNumbers, Args, RearrangedArgs),
	NewBase =.. [NewBaseName|RearrangedArgs].


get_newbasename(OriginalHead,IndexArgNumbers,Result) :-
	functor(OriginalHead,Functor,Arity),
	concat_atom([Functor,'_',Arity,'_newbase_',IndexArgNumbers],Result).


